package cn.mygweb.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.text.DecimalFormat;

/** 
* <p>Title: FileSizeUtil.java</p>
* <p>Description: 文件大小工具类</p>  
* @author gxb 
* @date 2019年8月5日 
* <p>Copyright: Copyright (c) 2019</p>
* <p>Company: www.mygweb.cn</p> 
*/
public class FileSizeUtil {
	
	public static final int SIZE_B = 1;
	public static final int SIZE_KB = 2;
	public static final int SIZE_MB = 3;
	public static final int SIZE_GB = 4;
	public static final int SIZE_TB = 5;	

	/**
	 * <p>Title: formatFileSize</p>
	 * <p>Description: 将字节数转换为大小值</p>
	 * @param size	原始字节大小
	 * @return	文件大小，类型为：B、KB、MB、GB、TB
	 */
	public static String formatFileSize(long size) {
		DecimalFormat df = new DecimalFormat("#.00");
		String fileSize = "";
		if(size == 0) {
			return "0B";
		}
		if(size < 1024) {
			fileSize = df.format((double)size) + "B";
		} else if(size < 1024 * 1024) {
			fileSize = df.format((double)size / 1024) + "KB";
		} else if(size < (1024 * 1024 * 1024L)) {
			fileSize = df.format((double)size / (1024 * 1024)) + "MB";
		} else if(size < (1024 * 1024 * 1024 * 1024L)) {
			fileSize = df.format((double)size / (1024 * 1024 * 1024L)) + "GB";
		} else {
			fileSize = df.format((double)size / (1024 * 1024 * 1024 * 1024L)) + "TB";
		}
		return fileSize;
	}
	
	/**
	 * <p>Title: formatFileSize</p>
	 * <p>Description: 转换文件大小，指定转换的类型</p>
	 * @param size	文件大小
	 * @param sizeType	转换类型
	 * @return	转换后的double类型文件大小
	 */
	public static double formatFileSize(long size, int sizeType) {
		DecimalFormat df = new DecimalFormat("#.00");
		double fileSize = 0;
		switch(sizeType) {
			case SIZE_B:
				fileSize = Double.valueOf(df.format((double)size));
				break;
			case SIZE_KB:
				fileSize = Double.valueOf(df.format((double)size / 1024));
				break;
			case SIZE_MB:
				fileSize = Double.valueOf(df.format((double)size / (1024 * 1024)));
				break;
			case SIZE_GB:
				fileSize = Double.valueOf(df.format((double)size / (1024 * 1024 * 1024L)));
				break;
			case SIZE_TB:
				fileSize = Double.valueOf(df.format((double)size / (1024 * 1024 * 1024 * 1024L)));
				break;
			default:
				break;
		}
		return fileSize;
	}
	
	/**
     * 	获取文件指定文件的指定单位的大小
     *
     * @param filePath 文件路径
     * @param sizeType 获取大小的类型1为B、2为KB、3为MB、4为GB
     * @return double值的大小
     */
    public static double getFileOrFilesSize(String filePath, int sizeType) {
        File file = new File(filePath);
        long blockSize = 0;
        try {
            if (file.isDirectory()) {
                blockSize = getFileSizes(file);
            } else {
                blockSize = getFileSize(file);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return formatFileSize(blockSize, sizeType);
    }

    /**
     * 	调用此方法自动计算指定文件或指定文件夹的大小
     *
     * @param filePath 文件路径
     * @return 计算好的带B、KB、MB、GB的字符串
     */
    public static String getAutoFileOrFilesSize(String filePath) {
        File file = new File(filePath);
        long blockSize = 0;
        try {
            if (file.isDirectory()) {
                blockSize = getFileSizes(file);
            } else {
                blockSize = getFileSize(file);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return formatFileSize(blockSize);
    }

    /**
     * 	获取指定文件大小
     */
    private static long getFileSize(File file) throws Exception {
        long size = 0;
        if (file.exists()) {
            FileInputStream fis = null;
            fis = new FileInputStream(file);
            size = fis.available();
        } else {
            file.createNewFile();
        }
        return size;
    }

    /**
     * 	获取指定文件夹
     */
    private static long getFileSizes(File f) throws Exception {
        long size = 0;
        File flist[] = f.listFiles();
        assert flist != null;
        for (File file : flist) {
            if (file.isDirectory()) {
                size = size + getFileSizes(file);
            } else {
                size = size + getFileSize(file);
            }
        }
        return size;
    }
    
    public static void main(String[] args) {
		String path = "D:\\JAVA\\tomcat";
    	double size1 = getFileOrFilesSize(path,3);
    	System.out.println("size1: "+size1);
    	String size2 = getAutoFileOrFilesSize(path);
    	System.out.println("size2: "+size2);
	}
}
